package com.example.controller;
import com.example.common.Result;
import com.example.mapper.QrcodeProductMapper;
import com.example.model.Machine;
import com.example.model.MachineTx;
import com.example.model.Machine_test;
import com.example.model.QrcodeProduct;
import com.example.service.MachineService;
import com.example.utils.JacksonUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


/**
 * @ClassName SysUserController
 * @Description 用户信息Controller层
 * @Author chengjunyu
 * @Date 2022/1/24 11:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/data")
public class MachineTxController {
    @Resource
    public MachineService machineService;

    @Resource
    public QrcodeProductMapper qrcodeProductMapper;

    @PostMapping("/insertMachineTx")
    public Result insertMachineTx(@RequestBody MachineTx machineTx) {
       // System.out.println(machineTx);
        Machine machine = new Machine();

        String str_qrcode = machineTx.getQrcode();
        String str_machine_id = machineTx.getMachine_id();
        String str_result = machineTx.getResult();
        //将List<Machine_test>转成String
        List<Machine_test> machine_test_List =  machineTx.getMachine_data();
        String str_machine_test = JacksonUtil.bean2Json(machine_test_List);
      //  String str_product_no = machineService.calc_product(str_qrcode);
       // String str_item_no = machineService.calc_item(str_product_no);
       // String str_colour_no = machineService.calc_colour(str_product_no);
        String str_machine_value = machineTx.getMachine_value();
        String str_machine_type = machineTx.getMachine_type();

        machine.setQrcode(str_qrcode);
        machine.setMachine_id(str_machine_id);
        machine.setResult(str_result);
        machine.setMachine_test(str_machine_test);
        machine.setMachine_value(str_machine_value);
        machine.setMachine_type(str_machine_type);



      //  machine.setProduct_no(str_product_no);
      //  machine.setItem_no(str_item_no);
     //   machine.setColour_no(str_colour_no);


      // System.out.println("Machine_data：123456789"+str_machine_data);
        // 从冠影CCD获取长度判断 A,B,C,D
      //  if ((str_machine_type.equals("GYCCD"))||(str_machine_type.equals("QCCCD")))
      //      machine.setLength_type(calc_length_type(machine_test_List,str_qrcode));
        //从天行CCD获取厚度判断 0,1
      //  if (str_machine_type.equals("TXCCD"))
      //     machine.setThickness_type(calc_thickness_type(machine_test_List));
        //写入结果
        //machineService.insertMachineResult(machine);

        //System.out.println(machine);
        int count = machineService.insertMachine(machine,machine_test_List);
        if(count > 0) {
            return Result.ok("新增记录成功。。。");
        }
        return Result.error("新增记录失败。。。");
    }


    /**
     * [{"TestPoint":"Q1","Value":"0.349"},{"TestPoint":"Q2","Value":"0.359"},{"TestPoint":"Q3","Value":"0.351"}]
     * @param machine_datalist
     * @return
     */
    private String calc_length_type374(List<Machine_test> machine_datalist)
    {
        AtomicReference<Float> Q1_length = new AtomicReference<>((float) 0);
        AtomicReference<Float> Q2_length = new AtomicReference<>((float) 0);
        AtomicReference<Float> Q3_length = new AtomicReference<>((float) 0);
        String length_type = "";

        //Q1  {"TestPoint":"Q1","Value":"0.349"}
        List<Machine_test> mid_resultList = machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"Q1")).collect(Collectors.toList());
        mid_resultList.stream().forEach(machine_test ->{
            Q1_length.set(Float.parseFloat(machine_test.getValue()));
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });
        //Q2  {"TestPoint":"Q2","Value":"0.359"}
        List<Machine_test> left_resultList = machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"Q2")).collect(Collectors.toList());
        left_resultList.stream().forEach(machine_test ->{
            Q2_length.set(Float.parseFloat(machine_test.getValue()));
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });
        //Q3  {"TestPoint":"Q3","Value":"0.351"}
        List<Machine_test> right_resultList = machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"Q3")).collect(Collectors.toList());
        right_resultList.stream().forEach(machine_test ->{
            Q3_length.set(Float.parseFloat(machine_test.getValue()));
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });
        if (   ((0.340f <= Q1_length.get()) && (Q1_length.get() <= 0.380f)) //S
                &&((0.340f <= Q2_length.get()) && (Q2_length.get() <= 0.380f))//S
                &&((0.340f <= Q3_length.get()) && (Q3_length.get() <= 0.380f))//S

                || ((0.340f <= Q1_length.get()) && (Q1_length.get() <= 0.380f))//S
                &&((0.340f <= Q2_length.get()) && (Q2_length.get() <= 0.380f))//S
                &&((0.381f <= Q3_length.get()) && (Q3_length.get() <= 0.420f))//L

                || ((0.340f <= Q1_length.get()) && (Q1_length.get() <= 0.380f))//S
                &&((0.381f <= Q2_length.get()) && (Q2_length.get() <= 0.420f))//L
                &&((0.340f <= Q3_length.get()) && (Q3_length.get() <= 0.380f))//S

                || ((0.381f <= Q1_length.get()) && (Q1_length.get() <= 0.420f))//L
                &&((0.340f <= Q2_length.get()) && (Q2_length.get() <= 0.380f))//S
                &&((0.340f <= Q3_length.get()) && (Q3_length.get() <= 0.380f))//S
        )
        {
            length_type = "A";
        }
        else if( ((0.381f <= Q1_length.get()) && (Q1_length.get() <= 0.420f)) //L
                &&((0.381f <= Q2_length.get()) && (Q2_length.get() <= 0.420f))//L
                &&((0.340f <= Q3_length.get()) && (Q3_length.get() <= 0.380f))//S

                || ((0.381f <= Q1_length.get()) && (Q1_length.get() <= 0.420f))//L
                &&((0.340f <= Q2_length.get()) && (Q2_length.get() <= 0.380f))//S
                &&((0.381f <= Q3_length.get()) && (Q3_length.get() <= 0.420f))//L

                || ((0.340f <= Q1_length.get()) && (Q1_length.get() <= 0.380f))//S
                &&((0.381f <= Q2_length.get()) && (Q2_length.get() <= 0.420f))//L
                &&((0.381f <= Q3_length.get()) && (Q3_length.get() <= 0.420f))//L

                || ((0.381f <= Q1_length.get()) && (Q1_length.get() <= 0.420f))//L
                &&((0.381f <= Q2_length.get()) && (Q2_length.get() <= 0.420f))//L
                &&((0.381f <= Q3_length.get()) && (Q3_length.get() <= 0.420f))//L
        )
        {
            length_type = "C";
        }

        return length_type;
    }
    /**
     * @ClassName calc_length_type
     * @Description
     * 按照长度判断A,B,C,D
     *二维码前十位 010:1031010090,1031010120/020:1031010101,1031010121
     * @Author lizhipeng
     * @Date 2023/6/9 08:41
     * @Version 1.0
     * 中长  {'TestPoint':'距离2-10','Value':'114.891'}
     * 左长  {'TestPoint':'距离1-11.2','Value':'111.37'}
     * 右长  {'TestPoint':'距离3-11.1','Value':'111.379'}
     */
    /*
    private String calc_length_type(List<Machine_test> machine_datalist, String str_qrcode)
    {
        String str_item = "";
        String str_product = "";
        AtomicReference<Float> left_length = new AtomicReference<>((float) 0);
        AtomicReference<Float> mid_length = new AtomicReference<>((float) 0);
        AtomicReference<Float> right_length = new AtomicReference<>((float) 0);
        String length_type = "";

       if ((str_qrcode.length() == 27) || (str_qrcode.length() == 26))
       {  //二维码前十位判断项目
           // str_item 010:1031010090,1031010120/020:1031010101,1031010121
           //010的料号(1031010090,1031010120)   1031010101
           str_product  = str_qrcode.substring(0, 10);
           if ((str_product.equals("1031010090")) || (str_product.equals("1031010120")))
            str_item = "ZS-D-010";
           //020的料号(1031010101,1031010121)
           if((str_product.equals("1031010101")) || (str_product.equals("1031010121")))
           str_item = "ZS-D-020";
       }
        if ((str_qrcode.length() == 15) || (str_qrcode.length() == 16))
        {//二维码第3位判断项目 010 :C   020 :F
            if (str_qrcode.substring(2, 3).equals("C"))
                str_item = "ZS-D-010";

            if (str_qrcode.substring(2, 3).equals("F"))
                str_item = "ZS-D-020";
        }

        //中长  {'TestPoint':'中长-3','Value':'114.891'}
        List<Machine_test> mid_resultList = machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"中长-3")).collect(Collectors.toList());
        mid_resultList.stream().forEach(machine_test ->{
            mid_length.set(Float.parseFloat(machine_test.getValue()));
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });
        //左长  {'TestPoint':'左长-4.2','Value':'111.37'}
        List<Machine_test> left_resultList = machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"左长-4.2")).collect(Collectors.toList());
        left_resultList.stream().forEach(machine_test ->{
            left_length.set(Float.parseFloat(machine_test.getValue()));
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });
        //右长  {'TestPoint':'右长-4.1','Value':'111.379'}
        List<Machine_test> right_resultList = machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"右长-4.1")).collect(Collectors.toList());
        right_resultList.stream().forEach(machine_test ->{
            right_length.set(Float.parseFloat(machine_test.getValue()));
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });

       if (str_item.equals("ZS-D-010"))
       {
           //010 A类 (中长117.99~118.07)并且(左长114.39~114.47)并且(右长114.39~114.47)
           if ( ((117.99f <= mid_length.get()) && (mid_length.get() <= 118.07f))
              &&((114.39f <= left_length.get()) && (left_length.get() <= 114.47f))
              &&((114.39f <= right_length.get()) && (right_length.get() <= 114.47f)))
           {
               length_type = "A";
           }
           //010 B类 (中长117.99~118.09)并且(左长114.39~114.49)并且(右长114.39~114.49)
           else if( ((117.99f <= mid_length.get()) && (mid_length.get() <= 118.09f))
                   &&((114.39f <= left_length.get()) && (left_length.get() <= 114.49f))
                   &&((114.39f <= right_length.get()) && (right_length.get() <= 114.49f)))
           {
               length_type = "B";
           }
           //010 C类 (中长117.96~118.07)并且(左长114.36~114.47)并且(右长114.36~114.47)
           else if ( ((117.96f <= mid_length.get()) && (mid_length.get() <= 118.07f))
                   &&((114.36f <= left_length.get()) && (left_length.get() <= 114.47f))
                   &&((114.36f <= right_length.get()) && (right_length.get() <= 114.47f)) )
           {
               length_type = "C";
           }
           //010 D类 (中长117.96~118.09)并且(左长114.36~114.49)并且(右长114.36~114.49)
           else if (  ((117.96f <= mid_length.get()) && (mid_length.get() <= 118.09f))
                   &&((114.36f <= left_length.get()) && (left_length.get() <= 114.49f))
                   &&((114.36f <= right_length.get()) && (right_length.get() <= 114.49f))   )
           {
               length_type = "D";
           }

       }
       else if (str_item.equals("ZS-D-020"))
        {
            //020 A类 (中长114.83~114.91)并且(左长111.32~111.40)并且(右长111.32~111.40)
            if ( ((114.83f <= mid_length.get()) && (mid_length.get() <= 114.91f))
                    &&((111.32f <= left_length.get()) && (left_length.get() <= 111.40f))
                    &&((111.32f <= right_length.get()) && (right_length.get() <= 111.40f)))
            {
                length_type = "A";
            }
            //020 B类 (中长114.83~114.93)并且(左长111.32~111.42)并且(右长111.32~111.42)
            else if( ((114.83f <= mid_length.get()) && (mid_length.get() <= 114.93f))
                    &&((111.32f <= left_length.get()) && (left_length.get() <= 111.42))
                    &&((111.32f <= right_length.get()) && (right_length.get() <= 111.42f)))
            {
                length_type = "B";
            }
            //020 C类 (中长114.80~114.91)并且(左长111.29~111.40)并且(右长111.29~111.40)
            else if ( ((114.80f <= mid_length.get()) && (mid_length.get() <= 114.91f))
                    &&((111.29f <= left_length.get()) && (left_length.get() <= 111.40))
                    &&((111.29f <= right_length.get()) && (right_length.get() <= 111.40f)))
            {
                length_type = "C";
            }
            //020 D类  (中长114.80~114.93)并且(左长111.29~111.42)并且(右长111.29~111.42)
            else if (  ((114.80f <= mid_length.get()) && (mid_length.get() <= 114.93f))
                    &&((111.29f <= left_length.get()) && (left_length.get() <= 111.42f))
                    &&((111.29f <= right_length.get()) && (right_length.get() <= 111.42f))  )
            {
                length_type = "D";
            }
        }

        return length_type;
    }

*/
    /**
     * @ClassName calc_thickness_type
     * @Description
     * 按照厚度判断 0,1
     * 0: 0.55 <a =<0.599
     * 1: 0.599<a =<0.65
     * @Author lizhipeng
     * @Date 2023/6/9 08:41
     * @Version 1.0
     */
    private String calc_thickness_type(List<Machine_test> machine_datalist)
    {
        AtomicReference<String> thickness_type = new AtomicReference<>("");
        List<Machine_test> resultList =
                machine_datalist.stream().filter(Machine_test -> Objects.equals(Machine_test.getTestPoint(),"厚度分类")).collect(Collectors.toList());
        resultList.stream().forEach(machine_test ->{
            thickness_type.set(machine_test.getValue());
            // System.out.println(machine_data.getTestPoint()+"-"+machine_data.getValue());
        });
        return thickness_type.get();
    }

    /**
     * @ClassName containsByField
     * @Description List 根据实体属性搜索
     * @Author chengjunyu
     * @Date 2022/1/24 11:18
     * @Version 1.0
     */
    public static <T> List<T> containsByField(List<T> list ,String field,Object value,Class<T> clazz){
        List<T> res = new ArrayList<>();

        try {
            // 通过反射获取对应的属性
            Field dbField = clazz.getDeclaredField(field);
            // 设置属性为可访问
            dbField.setAccessible(true);
            for (T t : list) {
                // 遍历list,获取到对应属性的值
                Object o = dbField.get(t);
                if(value.equals(o)){
                    // 如值和预期想等的话,则加入到返回值中
                    res.add(t);
                }
            }
        } catch (NoSuchFieldException e) {
            // 如果不存在这个字段,则直接返回空
            return res;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return res;
    }


};


